package com.study.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;
import com.study.bean.Address;
import com.study.bean.Users;
import com.study.dao.Addressjsdao;

/**閸︽澘娼�
 * Servlet implementation class addressservlet
 */
@WebServlet("/address")
public class addressservlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	Addressjsdao addressdao=new Addressjsdao();  
	@Override
	//鐠佸墽鐤嗘妯款吇
	public String excute(HttpServletRequest request, HttpServletResponse response) throws Exception {
		//闁俺绻冮悽銊﹀煕id閼惧嘲褰囬崷鏉挎絻娣団剝浼�
				Object id=session.getAttribute("uid");
		    	int ids=Integer.parseInt(id.toString());     		
		    	List<Address> addresslist=addressdao.yonghuById(ids);
		    	request.setAttribute("addresslist",addresslist);
		    	System.out.println(addresslist+"$$#####");  
		    	
		return "f:address";
}
	//鑾峰彇鍦板潃
	 public String huoqudizhi(HttpServletRequest request, HttpServletResponse response) throws Exception{
	    	Object id=session.getAttribute("uid");
	    	int ids=Integer.parseInt(id.toString());
	    	String addname=request.getParameter("addname");			//閸氬秴鐡�
	    	String provice=request.getParameter("provice");			//閻拷
	    	String city=request.getParameter("city");				//鐢拷
	    	String district=request.getParameter("district");		//閸橈拷
	    	String detailedaddress=request.getParameter("detailedaddress");		//鐠囷妇绮忛崷鏉挎絻
	    	String addphone=request.getParameter("addphone");		//閹靛婧�閸欙拷
	    	String addisdefault=request.getParameter("addisdefault");  	//閺勵垰鎯佹妯款吇	    	
	    	String addrssid=request.getParameter("dzbtnbc");		//闁俺绻冩稉濠氭暛閺夈儱鍨介弬顓熸煀婢х偞鍨ㄦ穱顔芥暭
	    	
	    	int addisdefaults=1;
	    	if(addisdefault!=null){
	    		addisdefaults=0;
	    		Addressjsdao tjaddressdao=new Addressjsdao();
	    		tjaddressdao.qbianUpdate();
	    		System.out.println("鏉╂稑鍙嗗鍝勫煑閸欐ê鑸伴崚銈嗘焽");
	    	}else{ 
	    		addisdefaults=1;
	    	}

	    	Addressjsdao tjaddressdao=new Addressjsdao();
	    	if(addrssid!=null&&!addrssid.equals("")){
	    		System.out.println(ids);
	    		Address xg=new Address();
	    		xg.setUserid(ids);
	    		xg.setAddname(addname);
	    		xg.setProvince(provice);
	    		xg.setCity(city);
	    		xg.setCounty(district);
	    		xg.setDetailedaddress(detailedaddress);
	    		xg.setAddphone(addphone);
	    		xg.setAddisdefault(addisdefaults);
	    		Boolean open=tjaddressdao.dizhiUpdate(xg, Integer.parseInt(addrssid));
	    	}else{
	    		Address hqaddresslist=new Address(null,ids,addname,provice,city,district,detailedaddress,addphone,addisdefaults);
	    		
	    		tjaddressdao.doInsert(hqaddresslist);
	    	}
	    		
	    	return "r:address";
	 }
	 //淇敼
	 public String xiugai(HttpServletRequest request, HttpServletResponse response) throws Exception{
	    	response.setContentType("application/json;charset=utf-8");
	    	System.out.println("娣囶喗鏁�");
	    	String addrssid=request.getParameter("addrssid");		//娑撳鏁�
	    	String xz=request.getParameter("xz");		//娑撳鏁�
	    	System.out.println(xz+"xxxxxxxxxxxx");
	    	int addrssids=Integer.parseInt(addrssid);
	    	System.out.println(addrssids);
	    	Addressjsdao dzaddressdao=new Addressjsdao();   	
	    	Address dzaddresslist=dzaddressdao.dizhiById(addrssids);
	    	System.out.println(JSON.toJSONString(dzaddresslist));
	    	/*PrintWriter out=response.getWriter();
	    	out.println(JSON.toJSONString(dzaddresslist));*/
			return JSON.toJSONString(dzaddresslist);    	
	    }
	 //鍒犻櫎
	    public String shanchu(HttpServletRequest request, HttpServletResponse response) throws Exception{
	    	response.setContentType("application/json;charset=utf-8");
	    	String addrssid=request.getParameter("addrssid");		//娑撳鏁�
	    	int addrssids=Integer.parseInt(addrssid);
	    	System.out.println("閸掔娀娅�");
	    	if(addrssid!=null){
	    		Addressjsdao addressdao=new Addressjsdao();   
	    		Address dzaddresslist=addressdao.dizhiById(addrssids);
	    		addressdao.dizhidelete(addrssids);
	        	return "{\"res\":true}";
	    	}   	
	    	return "{\"res\":false}";	
	    }
	    //璁剧疆榛樿
	    public String shezhi(HttpServletRequest request, HttpServletResponse response) throws Exception{
	    	response.setContentType("application/json;charset=utf-8");
	    	String addrssid=request.getParameter("addrssid");		//娑撳鏁�
	    	int addrssids=Integer.parseInt(addrssid);
	    	System.out.println("鐠佸墽鐤嗛崷鏉挎絻");
	    	if(addrssid!=null){
	    		Addressjsdao tjaddressdao=new Addressjsdao();
	    		tjaddressdao.qbianUpdate();
	    		tjaddressdao.zbianUpdate(addrssids);
	        	return "{\"res\":true}";
	    	}	
	    	return "{\"res\":false}";	
	    }
	 
}
